package org.soderiakelvy.controller.reparto;

import java.io.Serializable;

import java.util.List;
import java.util.Map;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.context.FacesContext;

import org.soderiakelvy.controller.util.Constante;
import org.soderiakelvy.controller.util.SelectOneMany;
import org.soderiakelvy.model.domain.PersonalVO;
import org.soderiakelvy.model.service.RepartoServicio;
import org.soderiakelvy.model.service.imp.RepartoServicioImp;
import org.soderiakelvy.model.util.ConstantModel;

@ManagedBean
public class PersonalList implements Serializable{
	private static final long serialVersionUID = 8984340441883965245L;
	
	private List<PersonalVO> personales;
	private List<PersonalVO> personalFilter;
	private RepartoServicio service;
	private PersonalVO personalSelected;
	
	//-- Constructor
	public PersonalList(){
		service = new RepartoServicioImp();
		getAllPersonal();
		System.out.println("Iniciando constructor personal list");
		FacesContext context = FacesContext.getCurrentInstance();
		context.getExternalContext().getSessionMap().remove("personalEdit");
	}
	
	private void getAllPersonal(){
		personales = SelectOneMany.getAllPersonal();
		personalFilter = SelectOneMany.getAllPersonal();
	}
	
	
	public String prepareEdit(){	
		FacesContext fc = FacesContext.getCurrentInstance();
		Map<String,Object> sessionParams = fc.getExternalContext().getSessionMap();
		System.out.println("Cantidad de sessiones: "+sessionParams.size()+ "Personal a Modificar: "+personalSelected.getNombre());
		sessionParams.put("personalEdit", personalSelected);
		return Constante.URL_PERSONAL_EDIT;
	}
	
	/**
	 * Elimina un personal.
	 * En el caso de existir un error muestra un mensaje por pantalla
	 */
	public void eliminarPersonal(){
		if(personalSelected != null){
			String result = service.deletePersonal(personalSelected);
			if(result.equals(ConstantModel.SUCCESS)){
				getAllPersonal();
			}else{
				if(result.equals(ConstantModel.DELETE_ASIGNADO)){
					FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_WARN,"No se puede eliminar el Personal porque tiene asignaciones", ""));
				}else {
					FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_FATAL,"Error al eliminiar un registro", "Surgio un error al eliminar un Personal. Comuicarse con el responsable del Sistema - "+result.toString()));
				}
			}
		}
	}
	
	/**
	 * Muestra la pagina web para cargar los datos del nuevo Personal
	 * @return URL de la pagina PersonalCreate
	 */
	public String nuevoPersonal(){
		return Constante.URL_PERSONAL_NEW;
	}
	
	public void bajaListener(PersonalVO p){
		this.personalSelected = p;
	}

	//--- GET & SET
	public List<PersonalVO> getPersonales() {
		return personales;
	}

	public PersonalVO getPersonalSelected() {
		return personalSelected;
	}

	public void setPersonalSelected(PersonalVO personalSelected) {
		this.personalSelected = personalSelected;
	}

	public void setPersonales(List<PersonalVO> personales) {
		this.personales = personales;
	}

	public List<PersonalVO> getPersonalFilter() {
		return personalFilter;
	}

	public void setPersonalFilter(List<PersonalVO> personalFilter) {
		this.personalFilter = personalFilter;
	}
	
	
}
